// 订单类，计算订单
class Order {
    constructor(array) {
        this.items = {}
    }

    // 初始化订单类
    initOrder(array, itemList, promotionList) {
        const obj = Order.parseInput(array)
        const itemIdList = Object.keys(obj)

        for (const e of itemIdList) {
            obj[e].name = itemList[e].itemName
            obj[e].price = itemList[e].price
            obj[e].promotion = itemList[e].promotion

            this.items[e] = Object.assign({}, obj[e])
        }

    }

    static parseInput(array) {
        let result = {}
        // array ["ITEM0013 x 4", "ITEM0022 x 1"]
        for (const e of array) {
            const id = e.slice(0, 8)
            const count = Number(e.slice(10))
            result[id] = {
                id: id,
                count: count
            }
        }
        return result

    }

    bestCharge(array, itemList, promotionList) {
        let result = ''
        this.initOrder(array, itemList)
        result = this.formatedString(this.items, itemList, promotionList)
        return result
    }

    // 原价
    originalPrice(items) {
        let r = 0
        const arr = Object.values(items)
        for (const e of arr) {
            r += e.count * e.price
        }
        return r
    }

    // 选择优惠及比价逻辑
    chosePromotion(items) {
        const originalPrice = this.originalPrice(items)
        const r = {}

        // 半价的菜品
        const halfPriceItem = Object.values(items).filter(e => e.promotion['halfPrice'] !== undefined)
        // 剩下的菜品
        const leftItem = Object.values(items).filter(e => e.promotion['halfPrice'] === undefined)

        const halfPriceItemLen = halfPriceItem.length
        const leftItemLen = leftItem.length

        // 半价的菜品价格
        let halfPrice = 0
        if (halfPriceItemLen !== 0) {
            for (const e of halfPriceItem) {
                halfPrice += e.count * e.price
            }
        }
        // 剩下的菜品价格
        let leftPrice = 0
        if (leftItemLen !== 0) {
            for (const e of leftItem) {
                leftPrice += e.count * e.price
            }
        }

        // 无满减且原价小于 30
        if (originalPrice < 30 && halfPriceItemLen === 0) {
            r.totalPrice = originalPrice
            r.promotionType = null
            return r
        }
        // 无满减且原价大于等于 30
        if (originalPrice >= 30 && halfPriceItemLen === 0) {
            r.totalPrice = Rule.fullDiscount(originalPrice)
            r.promotionType = 'fullDiscount'
            r.savedMoney = 6
            return r
        }
        // 有满减且原价小于 30
        if (originalPrice < 30 && halfPriceItemLen !== 0) {
            r.totalPrice = leftPrice + Rule.halfPrice(halfPrice)
            r.promotionType = 'halfPrice'
            r.savedMoney = originalPrice - r.totalPrice
            return r
        }

        const halfPriceSavedMoney = originalPrice - leftPrice - Rule.halfPrice(halfPrice)

        // 有满减且原价大于 30
        if (originalPrice > 30 && halfPriceItemLen !== 0) {
            // 满减省的多
            if (halfPriceSavedMoney < 6) {
                r.totalPrice = Rule.fullDiscount(originalPrice)
                r.promotionType = 'fullDiscount'
                r.savedMoney = 6
                return r
                // 半价省的多
            } else if (halfPriceSavedMoney > 6) {
                r.totalPrice = leftPrice + Rule.halfPrice(halfPrice)
                r.promotionType = 'halfPrice'
                r.savedMoney = originalPrice - r.totalPrice
                return r
                // 相等
            } else if (halfPriceSavedMoney === 6) {
                r.totalPrice = Rule.fullDiscount(originalPrice)
                r.promotionType = 'fullDiscount'
                r.savedMoney = 6
                return r
            }
        }
    }

    // 明细
    detail(items) {
        let r = ''
        const arr = Object.values(items)
        for (const e of arr) {
            r += `${e.name} x ${e.count} = ${e.count * e.price}元\n            `
        }
        // 切掉最后一个换行
        r = r.slice(0, r.length - 13)
        return r
    }

    // 优惠信息
    promotionMessage(items) {
        const s = '使用优惠:'

        const totalPrice = this.chosePromotion(items).totalPrice
        const savedMoney = this.chosePromotion(items).savedMoney

        const r = this.chosePromotion(items)

        if (r.promotionType === null) {
            return `总计：${totalPrice}元
            ===================================`
        } else if (r.promotionType === 'fullDiscount') {
            return s + `
            满30减6元，省${savedMoney}元
            -----------------------------------
            总计：${totalPrice}元
            ===================================`
        } else if (r.promotionType === 'halfPrice') {
            let nameList = ''
            const halfPriceItem = Object.values(items).filter(e => e.promotion['halfPrice'] !== undefined)
            for (const e of halfPriceItem) {
                nameList += e.name + '，'
            }
            // 切掉最后一个逗号
            nameList = nameList.slice(0, -1)
            return s + `
            指定菜品半价(${nameList})，省${savedMoney}元
            -----------------------------------
            总计：${totalPrice}元
            ===================================`
        }

    }

    // 字符串
    formatedString(items) {

        const detail = this.detail(items)
        const promotionMessage = this.promotionMessage(items)

        // log('detail=====', detail)
        let result = `
            ============= 订餐明细 =============
            ${detail}
            -----------------------------------
            ${promotionMessage}
    `

        return result

    }

    //     ============= 订餐明细 =============
    //     黄焖鸡 x 1 = 18元
    //     肉夹馍 x 2 = 12元
    //     凉皮 x 1 = 8元
    //     -----------------------------------
    //         使用优惠:
    //     指定菜品半价(黄焖鸡，凉皮)，省13元
    //     -----------------------------------
    //         总计：25元
    //     ===================================
}
